package com.siao.net.socket;

/**
 * @author SiaoLeon
 * Socket连接服务（抽象）
 */
abstract class ConnectionService {

	/**
	 * 缓存大小
	 */
	public Integer bufferSize = 1024 * 16;

	/**
	 * 连接处理器
	 */
	public IConnectionHandler connectionHandler;

	/**
	 * 数据解析器
	 */
	public IDataResolver dataResolver;

	/**
	 * 注册连接
	 * @param connection
	 */
	protected void connectionRegister(Connection connection) {
		if (this.connectionHandler != null) {
			this.connectionHandler.onConnected(connection);
		}
	}

	/**
	 * 连接失败
	 * @param connection
	 */
	protected void connectionFailed(Connection connection) {
		if (this.connectionHandler != null) {
			this.connectionHandler.onConnectFailed(connection);
		}
	}

	/**
	 * 发送数据
	 * @param connection
	 * @param data
	 */
	protected void connectionSend(Connection connection, IDataPack data) {
		if (this.connectionHandler != null) {
			this.connectionHandler.onSend(connection, data);
		}
	}

	/**
	 * 接收数据
	 * @param connection
	 * @param data
	 */
	protected void connectionReceive(Connection connection, Object data) {
		if (this.connectionHandler != null) {
			this.connectionHandler.onReceive(connection, data);
		}
	}

	/**
	 * 连接关闭
	 * @param connection
	 */
	protected void connectionClose(Connection connection) {
		if (this.connectionHandler != null) {
			this.connectionHandler.onClose(connection);
		}
	}

	/**
	 * 连接断开
	 * @param connection
	 */
	protected void connectionDisconnect(Connection connection) {
		if (this.connectionHandler != null) {
			this.connectionHandler.onDisconnect(connection);
		}
	}

}
